package com.miaosha;

import com.miaosha.domain.User;
import com.miaosha.result.CodeMessage;
import com.miaosha.result.Result;
import com.miaosha.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

@SpringBootApplication
@Controller
@RestController
@RequestMapping("/demo")
public class MainApplication {
    public static void main(String[] args) {
        SpringApplication.run(com.miaosha.MainApplication.class, args);
    }

    @RequestMapping("/")
    public String hello(@RequestParam(value = "name", defaultValue = "World") String name) {
        return String.format("Hello %s!", name);
    }

    @GetMapping("/result")
    public Result<String> result() {
        return Result.success("resulterror");
    }

    @GetMapping("/resulterror")
    public Result<String> resulterror() {
        return Result.error(CodeMessage.SERVER_ERROR);
    }

    @GetMapping("/thymeleaf")
    public  String thymeleaf(Model model){
        model.addAttribute("name","info");
        return "hello";
    }
    @Autowired
    UserService userService;
    @GetMapping("/db/get")
    @ResponseBody
    public Result<User> dbGet(){
        User user = userService.getById(1);
        if (user.getName() != null){
            return Result.success(user);
        }else {
            return Result.error(CodeMessage.SERVER_ERROR);
        }
    }
    @GetMapping("db/tx")
    public Result<Boolean> dbTx(){
        userService.tx();
        return Result.success(true);
    }


}